package com.ruoyi.web.controller.admin.construction;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dto.ContructionProjectAddOrUpdateDTO;
import com.ruoyi.system.service.ConstructionProjectService;
import com.ruoyi.system.service.IProjectStageService;
import com.ruoyi.system.vo.ProjectDetailVO;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.system.dto.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 施工项目Controller（工程管理-项目施工-项目列表）
 */
@RestController
@RequestMapping("/admin/construction/project")
public class ConstructionProjectController extends BaseController {

    @Resource
    private ConstructionProjectService projectService;

    @Resource
    private IProjectStageService projectStageService;

    /**
     * 新增施工项目
     */
    @PostMapping("/add")
    @ApiOperation("新增施工项目")
    public AjaxResult addProject(
            @Validated @RequestBody ContructionProjectAddOrUpdateDTO dto
    ) {
        Long userId = SecurityUtils.getUserId();
        // 从请求头/上下文获取操作人（实际项目需结合权限框架，如Spring Security）
        String projectCode = projectService.addProject(dto, userId.toString());
        return AjaxResult.success("施工项目创建成功", projectCode);
    }

    /**
     * 修改施工项目
     */
    @PostMapping("/update")
    @ApiOperation("修改施工项目")
    public AjaxResult updateProject(
            @Validated @RequestBody ContructionProjectAddOrUpdateDTO dto
    ) {
        if (dto.getId() == null) {
            return AjaxResult.error("项目ID不能为空");
        }
        Long userId = SecurityUtils.getUserId();
        projectService.updateProject(dto, userId.toString());
        return AjaxResult.success("施工项目修改成功");
    }

    /**
     * 分页查询施工项目列表
     */
    @GetMapping("/page")
    @ApiOperation("分页查询施工项目列表")
    public AjaxResult queryProjectPage(ContructionProjectQueryDTO queryDTO) {
        IPage<ContructionProjectDTO> pageResult = projectService.queryProjectPage(queryDTO);
        return AjaxResult.success(pageResult);
    }

    /**
     * 获取项目详情
     */
    @GetMapping("/detail/{projectId}")
    @ApiOperation("获取项目详情")
    public AjaxResult getProjectDetail(@PathVariable Long projectId) {
        ProjectDetailVO detailVO = projectService.getProjectDetail(projectId);
        return AjaxResult.success(detailVO);
    }

    /**
     * 更新项目状态（进行中/暂停）
     */
    @PostMapping("/update-status")
    @ApiOperation("更新项目状态")
    public AjaxResult updateProjectStatus(
            @RequestParam Long projectId,
            @RequestParam Integer status,
            @RequestParam(required = false) String pauseReason
    ) {
        Long userId = SecurityUtils.getUserId();
        projectService.updateProjectStatus(projectId, status, pauseReason, userId.toString());
        return AjaxResult.success("项目状态更新成功");
    }

    /**
     * 根据施工阶段id查询关联项目
     */
    @GetMapping("/by-stage/{stageId}")
    @ApiOperation("根据施工阶段id查询关联项目")
    public AjaxResult getProjectsByStageId(@PathVariable Long stageId) {
        return AjaxResult.success(projectStageService.getProjectsByStageId(stageId));
    }

    /**
     * 根据施工阶段id查询未关联项目
     */
    @GetMapping("/not-in-stage/{stageId}")
    @ApiOperation("根据施工阶段id查询未关联项目")
    public AjaxResult getProjectsNotInStageId(@PathVariable Long stageId) {
        return AjaxResult.success(projectStageService.getUnlinkedProjectsByStageId(stageId));
    }
}